/**
 * 技师管理的modal
 * @author: minzhang
 * @update: 2017-03-17
 */

import {create, remove, update, detail, query, setProject } from '../services/roomsManage';
import {message} from 'antd';
import {parse} from 'qs';

const json = {
	num: 32,
	name: 'John Brown',
	time: 33,
	price: 33,
	type: '18305517359',
}

let projList = [];

for(let i = 0; i < 40; i++) {
	const value = {...json, key: i};
	projList.push(value);
}

export default {

	namespace: 'techManage',

	state: {
		total: 0,
		pageNumber: 1,
		loading: false,
		pageSize: 10,
		list: [{
			key: '1',
			name: 'John Brown',
			num: 32,
			phone: '18305517359',
			gender: '男',
			avatar: 'http://115.29.172.104/gimg/img/e1d062a7d660dc9a7c94b2813671780d',
			leave: false
		}, {
			key: '2',
			name: 'Jim Green',
			num: 42,
			phone: '18305517359',
			gender: '男',
			avatar: 'http://115.29.172.104/gimg/img/e1d062a7d660dc9a7c94b2813671780d',
			leave: false
		}, {
			key: '3',
			num: '3',
			age: 32,
			phone: '18305517359',
			gender: '男',
			avatar: 'http://115.29.172.104/gimg/img/e1d062a7d660dc9a7c94b2813671780d',
			leave: true
		}],
		modalVisible: false,
		modalLoading: false,
		modalType: 'create',
		btnLoading: false,
		projList: projList,
		projLoading: false,
		projModalVisible: false,
		projBtnLoading: false,
		selectedKeys: [],
		fileList: [{
      uid: -1,
      name: 'xxx.png',
      status: 'done',
      url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    }],
		item: {},
		query: {},
	},

	subscriptions: {
		setup({dispatch, history}) {
				history.listen(location => {
						if (location.pathname === '/tech-manage') {
//                    dispatch({
//                        type: 'query',
//                        payload: {
//                            pageNumber: 1,
//                            pageSize: 10,
//                        },
//                    });
						}
				});
		}
	},

	effects: {
		*query({payload}, {call, put, select}) {
			yield put({type: 'showLoading'});
			const querys = yield select(state => state.techManage.query);
			const {data} = yield call(query, parse({...querys, ...payload}));
			if (data && data.success) {
				
				const {list, total, pageNumber, pageSize} = data.data.data;

				yield put({
						type: 'querySuccess',
						payload: {
								list,
								total,
								pageNumber,
								pageSize,
						},
				});
			} else {
					message.error(data && data.message || '数据获取失败');
			}
		},
		*'delete'({payload}, {call, put}) {
//			yield put({type: 'showLoading'});
//			const {id, pageNumber, pageSize} = payload;
//			const {data} = yield call(remove, id);
//			if (data && data.success) {
//					message.success('离职成功');
//					yield put({
//							type: 'query',
//							payload: {
//								pageNumber,
//								pageSize,
//							}
//					});
//			} else {
//					message.error(data && data.message || '数据获取失败');
//					yield put({type: 'hideLoading'});
//			}
		},
		*create({payload}, {call, put}) {
			yield put({type: 'showBtnLoading'});
			const {formData, fn, pageNumber, pageSize} = payload;
			const {data} = yield call(create, formData);
			if (data && data.success) {
					fn();
					message.success('保存成功');
					yield put({type: 'hideModal'});
					yield put({type: 'hideBtnLoading'});
					yield put({
							type: 'query',
							payload: {
									pageNumber,
									pageSize,
							},
					});
			} else {
					yield put({type: 'hideBtnLoading'});
					message.error(data && data.message || '数据获取失败');
			}
		},
		*update({payload}, {select, call, put}) {
			yield put({type: 'showBtnLoading'});
			const {formData, fn, pageNumber, pageSize} = payload;
			const {data} = yield call(update, formData);
			if (data && data.success) {
					fn();
					message.success('保存成功');
					yield put({type: 'hideModal'});
					yield put({type: 'hideBtnLoading'});
					yield put({
							type: 'query',
							payload: {
									pageNumber,
									pageSize,
							},
					});
			} else {
					message.error(data && data.message || '数据获取失败');
					yield put({type: 'hideBtnLoading'});
			}
		},
		*add({payload}, {select, call, put}) {
			yield put({
					type: 'showModal',
					payload: {
							modalType: 'create',
							modalLoading: true,
					}
			});
		},
		*detail({payload}, {select, call, put}) {
			yield put({
					type: 'showModal',
					payload: {
							modalType: payload.modalType,
							modalLoading: true
					}
			});
			const {id} = payload;
			const {data} = yield call(detail, id);
			if (data && data.success) {
					const res = data.data;

					let fileList = [];

//					if (res.avatar) {
//							// 处理图片文件格式
//							fileList = [{
//									uid: new Date().getTime(),
//									name: 'xxx.png',
//									status: 'done',
//									url: res.avatar,
//									thumbUrl: res.avatar,
//							}];
//					}

					yield put({
							type: 'updateAction',
							payload: {
									item: res,
									fileList,
							},
					});
			} else {
					message.error(data && data.message || '数据获取失败');
			}
		},
		*getProjList({payload}, {select, call, put}) {
			yield put({type: 'projShowLoading'});
			const {data} = yield call(query, parse({...querys, ...payload}));
//			if (data && data.success) {
//				const {list} = data.data.data;

//				yield put({
//						type: 'projQuerySuccess',
//						payload: {
//								projList: list,
//						},
//				});
//			} else {
//					message.error(data && data.message || '数据获取失败');
//			}
				
		},
		*setProject({payload}, {select, call, put}) {
			yield put({type: 'projShowBtnLoading'});
//			const {id} = payload;
//			const {data} = yield call(setProject, id);
//			if (data && data.success) {
//					yield put({type: 'projHideBtnLoading'});
//			} else {
//					message.error(data && data.message || '数据获取失败');
//			}
		},
	},

	reducers: {
		showLoading(state) {
			return {...state, loading: true};
		},
		hideLoading(state) {
			return {...state, loading: false};
		},
		showBtnLoading(state) {
			return {...state, btnLoading: true};
		},
		hideBtnLoading(state) {
			return {...state, btnLoading: false};
		},
		querySuccess(state, action) {
			return {...state, ...action.payload, loading: false};
		},
		showModal(state, action) {
			return {...state, ...action.payload, modalVisible: true};
		},
		hideModal(state) {
			return {
				...state,
				modalVisible: false,
				modalType: 'create',
				btnLoading: false,
				modalLoading: false,
				currentItem: {},
			};
		},
		projShowModal(state, action) {
			return {
				...state,
				...action.payload,
				projModalVisible: true,
				selectedRowKeys: [],
			};
		},
		projHideModal(state, action) {
			return {
				...state, 
				...action.payload, 
				projModalVisible: false, 
				projHideBtnLoading: false, 
				projLoading: false,
				projList: []
			};
		},
		projShowBtnLoading(state) {
			return {...state, projBtnLoading: true};
		},
		projHideBtnLoading(state) {
			return {...state, projBtnLoading: false};
		},
		projShowLoading(state) {
			return {...state, projLoading: true};
		},
		projHideLoading(state) {
			return {...state, projLoading: false};
		},
		projQuerySuccess(state, action) {
			return {...state, ...action.payload, projLoading: false};
		},
		updateAction(state, action) {
			return {...state, ...action.payload};
		},
	},
};
